<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="generator" content=
"HTML Tidy for Windows (vers 14 February 2006), see www.w3.org" />
<title>Reconditioned Machines</title>
<link rel="stylesheet" href="home.css" type="text/css" />
</head>
<body>
<center>

<?php
include 'menu.php';
include 'imgresize.php';
verifySession();
connectDB();

// Request the machine's data
$machineQuery = mysql_query(
		"SELECT * FROM reconditioned WHERE machineID =  " . $_GET['machineID']);
if (!$machineQuery) {
echo("<P>Error performing query: " . mysql_error() . "</P>");
exit();
}
$machineRow = mysql_fetch_array($machineQuery);

if($_POST["action"] == "Submit Changes")
{ 
	unset($imagename);
	if(!isset($_FILES) && isset($HTTP_POST_FILES))
		$_FILES = $HTTP_POST_FILES;
	if(!isset($_FILES['image_file']))
		$error["image_file"] = "An image was not found.";

	$imagename = basename($_FILES['image_file']['name']);
	//echo $imagename;
	if(empty($imagename))
		$error["imagename"] = "The name of the image was not found.";
	$picSQL = ""; //will not alter the picture if nothing was uploaded
	if(empty($error)) //picture exists, upload the pic as [cowtag].jpg and [cowtag]thumb.jpg
	{
		if ($machineRow["thumblink"] != "") unlink($machineRow["thumblink"]);
		if ($machineRow["piclink"] != "") unlink($machineRow["piclink"]);
		$newimage = "pictures/reconditioned/temp-import-pic.jpg";
		//echo $newimage;
		$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
		if(empty($result))
			$error["result"] = "There was an error moving the uploaded file.";

		$cowtag = ereg_replace("[^A-Za-z0-9]", "", $_POST["cowtag"]);  //format name

		$thumbfile = "pictures/reconditioned/" . $cowtag . "thumb.jpg";
		$thumb=new thumbnail($newimage);
		$thumb->size_height(60);
		if (file_exists($thumbfile)) unlink($thumbfile);
		$thumb->save($thumbfile);

		$zoomfile = "pictures/reconditioned/" . $cowtag . ".jpg";
		$zoom=new thumbnail($newimage);
		$zoom->size_auto(508);
		if (file_exists($zoomfile)) unlink($zoomfile);
		$zoom->save($zoomfile);

		unlink($newimage);
		$picSQL = "', thumblink = '" . $thumbfile . "', piclink = '" . $zoomfile;


	}
  $sql="UPDATE reconditioned SET category = '" . $_POST["category"] . 
  "', cowtag = '" . $_POST["cowtag"] . 
  "', description = '" . $_POST["description"] . 
  "', price = '" . $_POST["price"] . $picSQL .
  "' WHERE machineID = '" . $_GET['machineID'] . 
"'";
  //echo("<p>Here is the SQL:</p>");
  //echo("<p>" . $sql . "</p>");
  mysql_query($sql);

  header("location:reconditioned_admin.php");
}

echo("<p>Please make changes and click 'Update'.</p>");
echo("<p>(type '&amp;Oslash' for a '&Oslash' symbol)</p>");

// Request the categories list
  $cats = mysql_query("SELECT * FROM category_xref");
  if (!$cats) {
    echo("<P>Error performing query: " . mysql_error() . "</P>");
    exit();
  }
?>
<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?$_SERVER["PHP_SELF"];?>">
<p>Category: <select name='category'></p>
<?php
while ( $catRow = mysql_fetch_array($cats) ) {
  if ($machineRow[category] == $catRow[catCode])
    echo("<option SELECTED value='" . $catRow["catCode"] . "'>" . $catRow["Description"] . "</option>");
  else
    echo("<option value='" . $catRow["catCode"] . "'>" . $catRow["Description"] . "</option>");
}
echo("</select>");

?>

<table class="reconditioned" width="770px">
<tr>
<td class="majHeading" width="10%">UNIT #</td>
<td class="majHeading" width="75%">UNIT INFORMATION</td>
<td class="majHeading" width="10%">PRICE</td>
<td class="majHeading" width="5%">
<center><img src="images/photo.gif" alt="" /></center>
</td>
</tr>

<?php

echo("<tr><td><input size='5' name='cowtag' type='text' id='myCowtag' value='" . $machineRow["cowtag"] . "'></td>");
echo("<td><input size='60' name='description' type='text' id='myDescription' value='" . $machineRow["description"] . "'></td>");
echo("<td>$<input size='5' name='price' type='text' id=myPrice' value='" . $machineRow["price"] . "'></td>");
	if (!empty($machineRow["piclink"]) && !empty($machineRow["piclink"])) {
	  echo("<td align='center'><a href=\"javascript:PopupPic('" . $machineRow["piclink"] . "')\">");
	  echo("<img src=\"" . $machineRow["thumblink"] . "\" height='60' /></a></td></tr></table>");
	}
	else
	  echo("<td align='center'><img src='pictures/reconditioned/nopicthumb.jpg' height='60' /></td></tr></table>");

?>
<p>Change/Add photo (<b>**2MB max!!**</b>):<input type="file" name="image_file" size="10" /></p>
<p><input type="submit" value="Submit Changes" name="action"></p>
<a href='reconditioned_admin.php'> [Cancel]</a></p>

</center>
</body>
</html>